home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / objc.793 < prev    next >
Text File  |  1992-02-06  |  5KB  |  85 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f2\fmodern Courier;\f1\fswiss Helvetica;}
  2. \paperw12940
  3. \paperh9000
  4. \margl120
  5. \margr1000
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f0\b0\i0\ul0\fs28\fc0 multiple declarations setName name clash dynamic binding
  8. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  9. \
  10. Q:  I need to use the method 
  11. \b setName: 
  12. \b0 for both a Sound object and an NXImage object in the same file.  However, I always get this warning:\
  13. \
  14.      
  15. \f2\fs24\fc0 warning: multiple declarations for method `setName:'
  16. \f0\fs28  \
  17. \
  18. at compilation time.  In addition, occasionally the wrong method is called at run-time.  How can I get rid of the warning and make sure the correct method is always used?\
  19. \
  20. A:  The problem is caused by the fact that NXImage's 
  21. \b setName:
  22. \b0  method returns a BOOLEAN, while Sound's 
  23. \b setName:
  24. \b0  returns an 
  25. \b id
  26. \b0 .  You can avoid the problem by using static typing, instead of declaring the objects to be of type 
  27. \b id
  28. \b0 .  Static typing enables the compiler to do better type checking. The following code snippet shows how to do it:\
  29. \
  30.  
  31. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f2\fs24\fc0     Sound *mySound;        /* not: id mySound */\
  32.     NXImage *myImage;    /* not: id myImage */\
  33.     \
  34.     mySound = [[Sound alloc] init];\
  35.     myImage = [[NXImage alloc] init];\
  36.     [mySound setName:"The Beatles"];\
  37.     [myImage setName:"The Stars"];\
  38. \
  39.  
  40. \pard\tx740\tx1500\tx2260\tx3020\tx3780\tx4520\tx5280\tx6040\tx6800\tx7560\f0\fs28\fc0 If static typing is not possible, you can use type-casting instead:\
  41.  
  42. \f1\fs24 \
  43.  
  44. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f2\fc0     id mySound, myImage;\
  45.     \
  46.     mySound = [[Sound alloc] init];\
  47.     myImage = [[NXImage alloc] init];\
  48.     [(Sound *)mySound setName:"The Beatles"];\
  49.     [(NXImage *)myImage setName:"The Stars"];\
  50.  
  51. \f1\i \
  52.  
  53. \f2\i0     
  54. \f0\fs28 \
  55.  
  56. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 Note that to prevent similar problems when using duplicate method names for different classes, you should keep their argument types and return value types identical, unless you plan on using static typing to differentiate them.\
  57. \
  58.  
  59. \fc0 Why does the compiler not do better type checking by default?  The reason is that Objective-C uses dynamic binding (also known as "run-time binding") to give you greater flexibility in deciding which object will be sent a given message.  Dynamic binding means that the class of the message's recipient is determined by the Objective-C run-time system rather than by the compiler.  This facility lets you send the same message to any of several different classes of object (each declared as type 
  60. \b id
  61. \b0 ), depending on the current state of the application.  
  62. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 A good example of the potential of dynamic binding is InterfaceBuilder, which 
  63. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 can 
  64. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 manipulate many different 
  65. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 types of
  66. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0  object
  67. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 s
  68. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 , not all of which existed when InterfaceBuilder was co
  69. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 mpil
  70. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 ed.
  71. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 \
  72. \
  73. The compiler doesn't care if several different classes use exactly the same method name, because the class of the recipient won't be determined anyway until run-time; and as long as the class implements the method, all is well.  In your problematic case, however, the compiler can't figure out which of two similar method names is being invoked by your code, and it may decide on the wrong name.  \
  74. \
  75. For more on dynamic binding and Objective-C, see NeXTanswer objc.342.\
  76.  
  77. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320 \
  78.  
  79. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 QA793\
  80. \
  81. Not Valid for 1.0\
  82. Valid for 2.0\
  83. \
  84.  
  85.